package com.pda.yuanjing.pages.inbilln.list;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.pda.yuanjing.PDApp;
import com.pda.yuanjing.R;
import com.pda.yuanjing.beanout.BeanNOut;
import com.pda.yuanjing.database.DataBaseControl;
import com.pda.yuanjing.database.InStore;
import com.pda.yuanjing.pages.inbilln.add.BillAddActivity;
import com.pda.yuanjing.pages.inbilln.scanf.ScanfActivity;
import com.public_module.basemvp.activity.BaseActivity_noMVP;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BillnActivity extends BaseActivity_noMVP {
    private String TAG = "InBillnActivity";

    @BindView(R.id.tvhint)
    TextView tvhint;
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.rltitle)
    RelativeLayout rltitle;
    @BindView(R.id.login_id)
    EditText loginId;
    @BindView(R.id.loginbt)
    Button loginbt;
    @BindView(R.id.listview)
    SwipeMenuListView listview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.rl)
    LinearLayout rl;
    List<InStore> list = new ArrayList<>();
    List<BeanNOut> beanNOutList = new ArrayList<>();
    BillnAdapter bilLnAdapter = null;

    protected void initdata() {
        tvTitle.setText("离线入库");
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadMore(false);
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                SwipeMenuItem deleteItem = new SwipeMenuItem(getApplicationContext());
                deleteItem.setWidth(100);
                deleteItem.setTitle("删除");
                deleteItem.setTitleSize(16);
                deleteItem.setTitleColor(Color.WHITE);
                deleteItem.setBackground(R.color.red);
                menu.addMenuItem(deleteItem);
            }
        };
        listview.setMenuCreator(creator);
        listview.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        DataBaseControl.deleteInCode(beanNOutList.get(position).getBillID());
                        DataBaseControl.deleteInBill(beanNOutList.get(position).getBillID());
                        beanNOutList.remove(position);
                        bilLnAdapter = new BillnAdapter(BillnActivity.this, beanNOutList);
                        listview.setAdapter(bilLnAdapter);
                        list = DataBaseControl.getInBillAll();
                        if(list.size()==0){
                            tvhint.setVisibility(View.GONE);
                            refreshLayout.setVisibility(View.GONE);
                            rl.setVisibility(View.VISIBLE);
                        }
                        break;
                }
                return false;
            }
        });

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                intentToActivityWithSParameter(BillnActivity.this, ScanfActivity.class, PDApp.loacl, beanNOutList.get(position));
            }
        });
    }

    void formatList() {
        beanNOutList.clear();
        BeanNOut beanNOut = null;
        String id = "";
        for (int i = 0; i < list.size(); i++) {
            if (!id.equals(list.get(i).getBillID())) {
                id = list.get(i).getBillID();
                if (i > 0) {
                    beanNOutList.add(beanNOut);
                }
                beanNOut = new BeanNOut();
                beanNOut.setBillID(list.get(i).getBillID());
                beanNOut.setBillCode(list.get(i).getBillCode());
                beanNOut.setBillType(2);
                beanNOut.setDesStoreID(list.get(i).getDesStoreID());
                beanNOut.setDesStoreName(list.get(i).getDesStoreName());
                beanNOut.setCreateTime(list.get(i).getCreateTime());
                if(DataBaseControl.getInCodes(list.get(i).getBillID()).size()==0){
                    beanNOut.setState("未扫描");
                }else{
                    beanNOut.setState("已完成");
                }
                List<BeanNOut.DataBean> temps = new ArrayList<>();
                BeanNOut.DataBean temp = new BeanNOut.DataBean();
                temp.setBillListID(list.get(i).getBillListID());
                temp.setProductID(list.get(i).getProductID());
                temp.setProductName(list.get(i).getProductName());
                temp.setCnum(list.get(i).getCaseBoxNum());
                temp.setYnum(list.get(i).getYnum());
                temp.setSnum(list.get(i).getSnum());
                if(!beanNOut.getState().equals("未扫描") &&  list.get(i).getYnum()>list.get(i).getSnum()){
                    beanNOut.setState("部分扫描");
                }
                temps.add(temp);
                beanNOut.setDataBeanList(temps);
            } else {
                BeanNOut.DataBean temp = new BeanNOut.DataBean();
                temp.setBillListID(list.get(i).getBillListID());
                temp.setProductID(list.get(i).getProductID());
                temp.setProductName(list.get(i).getProductName());
                temp.setCnum(list.get(i).getCaseBoxNum());
                temp.setYnum(list.get(i).getYnum());
                temp.setSnum(list.get(i).getSnum());
                if(!beanNOut.getState().equals("未扫描") &&  list.get(i).getYnum()>list.get(i).getSnum()){
                    beanNOut.setState("部分扫描");
                }
                beanNOut.getDataBeanList().add(temp);
            }
        }
        beanNOutList.add(beanNOut);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inbilln);
        ButterKnife.bind(this);
        initdata();
    }

    @Override
    protected void onResume() {
        super.onResume();
        list = DataBaseControl.getInBillAll();
        if (list.size() > 0) {
            tvhint.setVisibility(View.VISIBLE);
            refreshLayout.setVisibility(View.VISIBLE);
            rl.setVisibility(View.GONE);
            formatList();
            bilLnAdapter = new BillnAdapter(this, beanNOutList);
            listview.setAdapter(bilLnAdapter);
        } else {
            tvhint.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.GONE);
            rl.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.iv_title_left, R.id.iv_title_right, R.id.loginbt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
            case R.id.iv_title_right:
                intentToActivityWithoutParameter(this, BillAddActivity.class);
                break;
            case R.id.loginbt:
                if (list == null || list.size() == 0) {
                    return;
                }
                formatList();
                if (loginId.getText().toString().length() == 0) {
                    bilLnAdapter = new BillnAdapter(this, beanNOutList);
                    listview.setAdapter(bilLnAdapter);
                } else {
                    List<BeanNOut> temps = new ArrayList<>();
                    for (int i = 0; i < beanNOutList.size(); i++) {
                        if (beanNOutList.get(i).getBillID().contains(loginId.getText().toString())) {
                            temps.add(beanNOutList.get(i));
                        }
                    }
                    beanNOutList.clear();
                    beanNOutList.addAll(temps);
                    bilLnAdapter = new BillnAdapter(this, beanNOutList);
                    listview.setAdapter(bilLnAdapter);
                }
                break;
        }
    }
}
